# -*- coding:utf-8 -*-

from datetime import datetime, time
from sqlalchemy import and_, delete, desc, func, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from {{ module_name }}.entity.do.{{ table_name }}_do import {{ table_name|snake_to_pascal_case }}
from {{ module_name }}.entity.vo.{{ table_name }}_vo import {{ table_name|snake_to_pascal_case }}PageModel, {{ table_name|snake_to_pascal_case }}Model
from utils.page_util import PageUtil


class {{ table_name|snake_to_pascal_case }}Dao:

    @classmethod
    async def get_by_id(cls, db: AsyncSession, {{ table_name }}_id: int) -> {{ table_name|snake_to_pascal_case }}:
        """根据主键获取单条记录"""
        {{ table_name }} = (((await db.execute(
                            select({{ table_name|snake_to_pascal_case }})
                            .where({{ table_name|snake_to_pascal_case }}.id == {{ table_name }}_id)))
                       .scalars())
                       .first())
        return {{ table_name }}

    """
    查询
    """
    @classmethod
    async def get_{{ table_name }}_list(cls, db: AsyncSession,
                             query_object: {{ table_name|snake_to_pascal_case }}PageModel,
                             data_scope_sql: str,
                             is_page: bool = False) -> list[{{ table_name|snake_to_pascal_case }}]:

        query = (
            select({{ table_name|snake_to_pascal_case }})
            .where(
                {% for column in columns %}
                {% if column.mysql_type in ["VARCHAR", "TEXT", "CHAR"] %}{{ table_name|snake_to_pascal_case }}.{{ column.name }}.like(f"%{query_object.{{ column.name }}}%") if query_object.{{ column.name }} else True,{% else %}{{ table_name|snake_to_pascal_case }}.{{ column.name }} == query_object.{{ column.name }} if query_object.{{ column.name }} else True,{% endif %}
                {% endfor %}
                eval(data_scope_sql),
            )
            .order_by(desc({{ table_name|snake_to_pascal_case }}.create_time))
            .distinct()
        )
        {{ table_name }}_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return {{ table_name }}_list


    @classmethod
    async def add_{{ table_name }}(cls, db: AsyncSession, add_model: {{ table_name|snake_to_pascal_case }}Model) -> {{ table_name|snake_to_pascal_case }}:
        """
        增加
        """
        {{ table_name }} =  {{ table_name|snake_to_pascal_case }}(**add_model.model_dump(exclude_unset=True))
        db.add({{ table_name }})
        await db.flush()
        return {{ table_name }}

    @classmethod
    async def edit_{{ table_name }}(cls, db: AsyncSession, edit_model: {{ table_name|snake_to_pascal_case }}Model, auto_commit: bool = True):
        """
        修改
        """
        edit_dict_data = edit_model.model_dump(exclude_unset=True)
        await db.execute(update({{ table_name|snake_to_pascal_case }}), [edit_dict_data])
        await db.flush()
        if auto_commit:
            await db.commit()
        return edit_model

    @classmethod
    async def del_{{ table_name }}(cls, db: AsyncSession, del_model: {{ table_name|snake_to_pascal_case }}Model, soft_del: bool = True, auto_commit: bool = True):
        """
        删除
        """
        if soft_del:
            await db.execute(update({{ table_name|snake_to_pascal_case }}).where({{ table_name|snake_to_pascal_case }}.id == del_model.id).values(del_flag='2'))
        else:
            await db.execute(delete({{ table_name|snake_to_pascal_case }}).where({{ table_name|snake_to_pascal_case }}.id == del_model.id))
        await db.flush()
        if auto_commit:
            await db.commit()
